<?php
/**
 * Created by PhpStorm.
 * User: Lenovo
 * Date: 2019/4/26
 * Time: 23:24
 */

namespace app\admin\model;

use think\Db;
use think\Session;

class Yuyue extends BaseModel
{
    protected $table='jc_yuyue';
    public function liushuiindex()
    {
        $rs=$this->alias('yu')
            ->join('jc_indent in','in.tid=yu.tid','left')
            ->join('jc_user us','us.uid=yu.uid','left')
            ->join('jc_zpaytype zt','zt.zid=yu.zid','left')
            ->where('yu.sid',3)
            ->order('yu.y_endtime desc')
            ->paginate(7);
        //dump($this->objectToarray($rs));die;
        if(input('sub')) {
            $where = array();
            $cateid = input('cateid');//下拉菜单
            $key = input('Key');//输入框
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
            if (isset($datemin) && $datemin != '') {
                $where['y_endtime'] = ['>=', $datemin . ' 00:00:00'];
            }
            if (isset($datemax) && $datemax != '') {
                $where['y_endtime'] = ['<=', $datemax . ' 23:59:59'];
            }
            if ($cateid == 'u_phone') {
                $where['u_phone'] = ['like', '%' . $key . '%'];
            } else if ($cateid == 'u_name') {
                $where['u_name'] = ['like', '%' . $key . '%'];
            } else if ($cateid == 't_number') {
                $where['t_number'] = ['like', '%' . $key . '%'];
            } else if ($cateid == 'zid') {
                if($key=='微信')
                {
                    $key=1;
                }
                elseif ($key=='支付宝')
                {
                    $key=2;
                }
                elseif ($key=='银行卡')
                {
                    $key=3;
                }
                $where['jc_yuyue.zid'] = ['like', '%' . $key . '%'];
            }
            $rs=$this->alias('yu')
                ->join('jc_indent in','in.tid=yu.tid','left')
                ->join('jc_user us','us.uid=yu.uid','left')
                ->join('jc_zpaytype zt','zt.zid=yu.zid','left')
                ->where('yu.sid',3)
                ->order('yu.y_endtime desc');
            return $rs=$rs->where($where)->paginate(7,false,['query'=>request()->param()]);
        }
        else
        {
            return $rs;
        }
    }
    //用户表表连接
    public function userYuyue(){
        return $this->belongsTo('User','uid','uid');
    }
    //支付方式
    public function zPayYuyue(){
        return $this->belongsTo('Zpaytype','zid','zid');
    }
    /*
 * 预约成功->改变会员的信息，例如支付方式
 *
 */
    public static function getUpdateYuyue(){
        $yid = input('yid');
        $b=self::with(['userYuyue','zPayYuyue'])
            ->where('yid',$yid)
            ->find();
        if (request()->isAjax()) {
            $data['zid'] = input('zid');
            $updateO = self::where('yid', $yid)
                ->update($data);
            if ($updateO) {
                return 1;
            } else {
                return 2;
            }
        } else {
            return $b;
        }
    }
    //预约成功
    public static function getYuyueIndex(){
        $gid = Session::get('admin_uid');
        $user = new User();
        $gl = new Guanli();
        $gl=$gl->where('id',$gid)->find();
        $u = $user->where('uid',$gl['uid'])->find();
        $where=[];
        if($u['daili']==1) {
            if(input('sub')){
                $where=[];
                $zid = input('zid');
                if($zid != ''){
                    $where['zid']=[
                        'like',
                        '%'.$zid.'%'
                    ];
                }
                $arr =self::alias('a')
                    ->join('jc_user b', 'a.uid=b.uid', 'left')
                    ->where('b.u_path', 'like', '%' . $u['uid'] . '%')
                    ->where($where)
                    ->paginate(12);
                return $arr;
            }
            $arr =self::alias('a')
                ->join('jc_user b', 'a.uid=b.uid', 'left')
                ->where('b.u_path', 'like', '%,' . $u['uid'] . '%,')
                ->paginate(12);
            return $arr;
        }else {
            if (input('sub')) {
                $where = [];
                $zid = input('zid');
                if ($zid != '') {
                    $where['zid'] = [
                        'like',
                        '%' . $zid . '%'
                    ];
                }
                $arr = self::with(['userYuyue', 'zPayYuyue'])->where($where)->order('yid', 'desc')->paginate(8);
                return $arr;
            }
            $yuyues = self::with(['userYuyue', 'zPayYuyue'])
                ->order('yid', 'desc')
                ->paginate(8);
            return $yuyues;
        }
    }
}